/**
 * ReportGear(2011)
 */
package com.reportgear.designer.editor.object;

import javax.swing.JComponent;
import javax.swing.event.ChangeEvent;

/**
 * 通用对象编辑器接口
 * 
 * @version 1.0 2011-4-26
 * @author <a herf="lexloo@gmail.com">lexloo</a>
 * @since Report 1.0
 */
public abstract class Editor extends JComponent {
	/**
	 * UID
	 */
	private static final long serialVersionUID = -7024561762547340551L;
	// 编辑器名称
	private String editorName;
	// 改变事件
	private ChangeEvent changeEvent = new ChangeEvent(this);

	public abstract Object getValue();

	public abstract boolean accept(Object value);

	public String getName() {
		return this.editorName;
	}

	public void setName(String name) {
		this.editorName = name;
	}

	public abstract void setValue(Object value);

	public void startEditing() {
		this.fireEditingStarted();
	}

	public void stopEditing() {
		this.fireEditingStopped();
	}

	public void cancelEditing() {
		this.fireEditingCanceled();
	}

	public void addEditorListener(EditorListener listener) {
		this.listenerList.add(EditorListener.class, listener);
	}

	public void removeEditorListener(EditorListener listener) {
		this.listenerList.remove(EditorListener.class, listener);
	}

	public EditorListener[] getEditorListeners() {
		return this.listenerList.getListeners(EditorListener.class);
	}

	protected void fireEditingStarted() {
		for (EditorListener lstnr : this.getEditorListeners()) {
			lstnr.editingStarted(this.changeEvent);
		}
	}

	protected void fireEditingStopped() {
		for (EditorListener lstnr : this.getEditorListeners()) {
			lstnr.editingStopped(this.changeEvent);
		}
	}

	protected void fireEditingCanceled() {
		for (EditorListener lstnr : this.getEditorListeners()) {
			lstnr.editingCanceled(this.changeEvent);
		}
	}
}
